package com.lishe.lishe.lishe.module.commonclass;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.lishe.lishe.R;

import java.util.List;

/**
 * Created by shino on 2017/11/9.
 * 搜索历史记录
 */

public class SearchHistoryAdapter extends RecyclerView.Adapter<SearchHistoryAdapter.Holder> {
    Context context;
    List<String> list;

    public SearchHistoryAdapter(Context context, List<String> list) {
        this.context = context;
        this.list = list;
    }

    @Override
    public Holder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_search, null);
        return new Holder(view);
    }

    @Override
    public void onBindViewHolder(final Holder holder, int position) {
        holder.text.setText(list.get(position));
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (itemOnclick != null) {
                    itemOnclick.onItemClick(holder.text, holder.text.getText().toString());
                }
            }
        });
        holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View view) {
                if (itemOnLongclick != null) {
                    itemOnLongclick.onItemLongClick(holder.text, holder.text.getText().toString());
                }
                return longBoolean;
            }
        });
    }

    @Override
    public int getItemCount() {
        return list == null ? 0 : list.size();
    }

    class Holder extends RecyclerView.ViewHolder {
        TextView text;

        public Holder(View itemView) {
            super(itemView);
            text = itemView.findViewById(R.id.text);
        }
    }

    HistoryItemOnclick itemOnclick;

    public interface HistoryItemOnclick {
        void onItemClick(View v, String text);
    }

    public void setOnItemClick(HistoryItemOnclick itemClick) {
        this.itemOnclick = itemClick;

    }

    HistoryItemOnLongclick itemOnLongclick;

    public interface HistoryItemOnLongclick {
        void onItemLongClick(View v, String text);
    }

    boolean longBoolean = false;

    public void setOnItemLongClick(HistoryItemOnLongclick itemLongClick, boolean longBoolean) {
        this.itemOnLongclick = itemLongClick;
        this.longBoolean = longBoolean;
    }
}
